package com.banmao.mall.service.admin.impl;

import com.banmao.mall.mapper.*;
import com.banmao.mall.model.bo.admin.admin.ChangePasswordBO;
import com.banmao.mall.model.pojo.Admin;
import com.banmao.mall.model.pojo.Permission;
import com.banmao.mall.model.vo.admin.DashboardVO;
import com.banmao.mall.model.vo.admin.user.InfoVO;
import com.banmao.mall.service.admin.AdminService;
import com.banmao.mall.util.ShiroMd5Util;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SystemMapper systemMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Override
    public Admin login(String username) {

        Admin loginAdmin = adminMapper.selectLoginAdminByUsername(username);

        return loginAdmin;
    }

    @Override
    public InfoVO info(Integer adminId) {
        // 根据ID查找管理员信息：username、avatar、role_ids
        Admin adminInfo = adminMapper.selectInfoByID(adminId);

        // 处理role_ids字符串为数组
        String roleIds = adminInfo.getRoleIds();
        String substring = roleIds.substring(1, (roleIds.length()-1));
        String[] rolesId = substring.split(",");

        // 根据roleID查找数据库中的roles 并存入数组中
        List<String> rolesList = new ArrayList<String>();
        String roleString = null;
        for (String roleId : rolesId) {
            roleString = adminMapper.selectRolesByRolesID(roleId);
            rolesList.add(roleString);
        }

        // 根据roleID查找数据库中permission表的permission字段
        List<String> permList = new ArrayList<String>();
        List<String> tempList = new ArrayList<String>();
        String permString = null;
        for (String roleId : rolesId) {
            // 根据roleId获取对应role的permission数组
            tempList = adminMapper.selectPermByRoleID(roleId);
            // 将permission数组放入permList
            for (String temp : tempList) {
                String api = permissionMapper.selectApiByPermission(temp);
                permList.add(api);
                permList.add(temp);
            }
        }
        //xujingwei

     //   permList.add("*");

        //
        // 将两个数组放入VO对象中
        InfoVO infoVO = new InfoVO();
        infoVO.setName(adminInfo.getUsername());
        infoVO.setAvatar(adminInfo.getAvatar());
        infoVO.setRoles(rolesList);
        infoVO.setPerms(permList);

        return infoVO;
    }

    @Override
    public DashboardVO dashboard() {

        // 查询数据库中goods表、user表、product表、order表的记录数
        Integer goodsTotal = goodsMapper.selectTotalCount();
        Integer userTotal = userMapper.selectTotalCount();
        Integer productTotal = goodsProductMapper.selectTotalCount();
        Integer orderTotal = orderMapper.selectTotalCount();

        DashboardVO dashboardVO = new DashboardVO();
        dashboardVO.setGoodsTotal(goodsTotal);
        dashboardVO.setUserTotal(userTotal);
        dashboardVO.setProductTotal(productTotal);
        dashboardVO.setOrderTotal(orderTotal);

        return dashboardVO;
    }

    @Override
    public int changePassword(ChangePasswordBO changePasswordBO) {

        String newPassword = changePasswordBO.getNewPassword();


        // 获取管理员ID
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer adminId = (Integer) session.getAttribute("adminId");

        if(adminId == null) {
            throw new UnauthenticatedException("未登录");
        }

        // 查询用户名
        String adminUsername = adminMapper.selectUsernameByID(adminId);

        // 查询原密码是否正确
        String oldPassword = adminMapper.selectOldPassword(adminId);
        String inputOldPassword = ShiroMd5Util.SysMd5(adminUsername, changePasswordBO.getOldPassword());
        if(!oldPassword.equals(inputOldPassword)) {
            return -1;
        }

        // md5加密
        newPassword = ShiroMd5Util.SysMd5(adminUsername, newPassword);

        // 更改密码
        int result = adminMapper.updatePassword(adminId, newPassword);

        return result;
    }

}
